﻿
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using 低频天线阵列相控阵信号发生系统SmartFASK12.Models;

namespace 低频天线阵列相控阵信号发生系统SmartFASK12.Controls
{
    public class MJietiao
    {
        private static MJietiao _instance = null;
        private Random _rand = new Random();
        private MJietiao()
        {

        }

        public static MJietiao Singleton()
        {
            if (_instance == null)
            {
                _instance = new MJietiao();
            }

            return _instance;
        }

        public MJietiaoShowResultFSK JietiaoFSK(List<double> yvalues, int samplefre, int fre1, int bandwidth1, int order1, int fre2, int bandwidth2, int order2, int shift, int signalFre, bool isGuiyi, bool isAutoFirst, int autoValue = 0)
        {
            MJietiaoShowResultFSK res = new MJietiaoShowResultFSK();

            res.YValues = yvalues;

            try
            {
                #region 根据采样率生成x数据
                List<double> xvalues = new List<double>();
                double interval = 1.0 / samplefre;
                double cx = 0;
                for (int i = 0; i < yvalues.Count; i++)
                {
                    xvalues.Add(cx);

                    cx += interval;
                }
                #endregion

                #region 初始化数据
                MDataModel pdata = new MDataModel();
                pdata.XValues = xvalues;
                pdata.YValues = yvalues;

                res.Times = xvalues;
                for (int i = 0; i < xvalues.Count; i++)
                {
                    res.Judge_X.Add(i);
                }
                #endregion

                #region 频率1处理
                if (true)
                {
                    MDataModel dd = MyMatlab.Filter(pdata, fre1, bandwidth1, samplefre, order1);

                    MDataModel fftdd = MyMatlab.FFT(dd.YValues, samplefre);

                    MDataModel hil = MyMatlab.Hilbert(dd);

                    pdata.DataFre1 = hil.YValues;

                    res.Orig_Y1 = dd.YValues;
                    res.FFT_X1 = fftdd.XValues;
                    res.FFT_Y1 = fftdd.YValues;
                    res.Bao_Y1 = hil.YValues;
                }
                #endregion

                #region 频率2处理
                if (true)
                {
                    MDataModel dd = MyMatlab.Filter(pdata, fre2, bandwidth2, samplefre, order2);

                    MDataModel fftdd = MyMatlab.FFT(dd.YValues, samplefre);

                    MDataModel hil = MyMatlab.Hilbert(dd);

                    pdata.DataFre2 = hil.YValues;

                    res.Orig_Y2 = dd.YValues;
                    res.FFT_X2 = fftdd.XValues;
                    res.FFT_Y2 = fftdd.YValues;
                    res.Bao_Y2 = hil.YValues;
                }
                #endregion

                #region 解调
                if (pdata.HasDataFre())
                {
                    int disnum = samplefre / signalFre;

                    pdata.Judgement(shift, disnum, isGuiyi, ref res);

                    res.AutoFirst(isAutoFirst, autoValue);
                }
                #endregion

            }
            catch
            {
                return null;
            }




            return res;
        }

        public MJietiaoShowResultFSK2 JietiaoFSK2(List<double> yvalues, int samplefre, int fre1, int bandwidth1, int order1, int fre2, int bandwidth2, int order2, int shift, int signalFre, bool isGuiyi, bool isAutoFirst, int autoValue = 0)
        {
            MJietiaoShowResultFSK2 res = new MJietiaoShowResultFSK2();

            res.YValues = yvalues;

            try
            {
                #region 根据采样率生成x数据
                List<double> xvalues = new List<double>();
                double interval = 1.0 / samplefre;
                double cx = 0;
                for (int i = 0; i < yvalues.Count; i++)
                {
                    xvalues.Add(cx);

                    cx += interval;
                }
                #endregion

                #region 初始化数据
                MDataModel pdata = new MDataModel();
                pdata.XValues = xvalues;
                pdata.YValues = yvalues;

                res.Times = xvalues;
                for (int i = 0; i < xvalues.Count; i++)
                {
                    res.Judge_X.Add(i);
                }
                #endregion

                #region 频率1处理
                if (true)
                {
                    MDataModel dd = MyMatlab.Filter(pdata, fre1 * 2, bandwidth1, samplefre, order1);

                    MDataModel fftdd = MyMatlab.FFT(dd.YValues, samplefre);

                    MDataModel hil = MyMatlab.Hilbert(dd);

                    pdata.DataFre1 = hil.YValues;

                    res.Orig_Y1 = dd.YValues;
                    res.FFT_X1 = fftdd.XValues;
                    res.FFT_Y1 = fftdd.YValues;
                    res.Bao_Y1 = hil.YValues;
                }
                #endregion

                #region 频率2处理
                if (true)
                {
                    MDataModel dd = MyMatlab.Filter(pdata, fre2 * 2, bandwidth2, samplefre, order2);

                    MDataModel fftdd = MyMatlab.FFT(dd.YValues, samplefre);

                    MDataModel hil = MyMatlab.Hilbert(dd);

                    pdata.DataFre2 = hil.YValues;

                    res.Orig_Y2 = dd.YValues;
                    res.FFT_X2 = fftdd.XValues;
                    res.FFT_Y2 = fftdd.YValues;
                    res.Bao_Y2 = hil.YValues;
                }
                #endregion

                #region 解调
                if (pdata.HasDataFre())
                {
                    int disnum = samplefre / signalFre;

                    pdata.Judgement2(shift, disnum, isGuiyi, ref res);

                    res.AutoFirst(isAutoFirst, autoValue);
                }
                #endregion

            }
            catch
            {
                return null;
            }




            return res;
        }

        public MJietiaoShowResultFSK JietiaoFSK2(List<double> orgYvaluess, int samplefre, int fre1, int bandwidth1, int order1, int fre2, int bandwidth2, int order2, int shift, int signalFre, bool isGuiyi, bool isAutoFirst, int autovalue, double midValue, double checkValue)
        {
            MJietiaoShowResultFSK res = new MJietiaoShowResultFSK();

            try
            {
                #region 找到有效的数据段
                List<double> effyvalues = new List<double>();

                int checkCntStart = (int)(samplefre * 0.12 / fre2);
                if (checkCntStart < 2)
                {
                    checkCntStart = 2;
                }

                int checkCntEnd = samplefre / fre2;
                double maxCheck = midValue + checkValue;
                double minCheck = midValue - checkValue;

                bool isFindStart = false;
                for (int i = 0; i < orgYvaluess.Count; i++)
                {
                    if (!isFindStart)
                    {
                        bool isOk = true;
                        for (int k = 0; k < checkCntStart; k++)
                        {
                            if (orgYvaluess[i + k] < maxCheck && orgYvaluess[i + k] > minCheck)
                            {
                                isOk = false;
                                break;
                            }
                        }

                        if (isOk)
                        {
                            isFindStart = true;

                            effyvalues.Add(orgYvaluess[i]);
                        }
                    }
                    else
                    {
                        bool isEnd = true;
                        for (int k = 0; k < checkCntEnd; k++)
                        {
                            if (orgYvaluess[i + k] >= maxCheck || orgYvaluess[i + k] <= minCheck)
                            {
                                isEnd = false;
                                break;
                            }
                        }

                        if (!isEnd)
                        {
                            effyvalues.Add(orgYvaluess[i]);
                        }
                        else
                        {
                            break;
                        }

                    }
                }

                #endregion

                res = JietiaoFSK(effyvalues, samplefre, fre1, bandwidth1, order1, fre2, bandwidth2, order2, shift, signalFre, isGuiyi, isAutoFirst, autovalue);

            }
            catch
            {
                return null;
            }




            return res;
        }

        //优化尾部有一条线的情形，换一个算法
        public MJietiaoShowResultFSK JietiaoFSK3(List<double> orgYvaluess, int samplefre, int fre1, int bandwidth1, int order1, int fre2, int bandwidth2, int order2, int shift, int signalFre, bool isGuiyi, bool isAutoFirst, int autovalue, double midValue, double checkValue)
        {
            MJietiaoShowResultFSK res = new MJietiaoShowResultFSK();

            try
            {
                #region 找到有效的数据段
                //List<double> effyvalues = new List<double>();

                //int checkCntEnd = samplefre / fre2;
                //double maxCheck = midValue + checkValue;
                //double minCheck = midValue - checkValue;
                //List<double> org = orgYvaluess.Where(w=>w > minCheck && w< maxCheck).ToList();
                //bool isFindStart = false;
                //for (int i = 0; i < orgYvaluess.Count; i++)
                //{
                //    if (!isFindStart)
                //    {
                //        //bool isOk = true;
                //        double cmax = -10000;
                //        double cmin = 100000;
                //        for (int k = 0; k < checkCntEnd; k++)
                //        {
                //            if(i+k>=orgYvaluess.Count)
                //            {
                //                break;
                //            }
                //            if (orgYvaluess[i + k]>cmax)
                //            {
                //                cmax = orgYvaluess[i + k];
                //            }
                //            if(orgYvaluess[i+k]<cmin)
                //            {
                //                cmin = orgYvaluess[i + k];
                //            }
                //        }

                //        if (cmax-cmin>=checkValue)
                //        {
                //            isFindStart = true;

                //            effyvalues.Add(orgYvaluess[i+checkCntEnd-1]);
                //        }
                //    }
                //    else
                //    {

                //        double cmax = -1000;
                //        double cmin = 100000;
                //        for (int k = 0; k < checkCntEnd; k++)
                //        {
                //            if(i+k>=orgYvaluess.Count)
                //            {
                //                break;
                //            }

                //            if (orgYvaluess[i + k] > cmax)
                //            {
                //                cmax = orgYvaluess[i + k];
                //            }
                //            if (orgYvaluess[i + k] < cmin)
                //            {
                //                cmin = orgYvaluess[i + k];
                //            }
                //        }

                //        if (cmax-cmin>checkValue)
                //        {
                //            effyvalues.Add(orgYvaluess[i]);
                //        }
                //        else
                //        {
                //            break;
                //        }

                //    }
                //}



                #endregion

                #region 找到有效的数据段2

                List<double> effyvalues = new List<double>();
                effyvalues = orgYvaluess.Skip(40000).Take(80000).ToList();

                #endregion

                res = JietiaoFSK(effyvalues, samplefre, fre1, bandwidth1, order1, fre2, bandwidth2, order2, shift, signalFre, isGuiyi, isAutoFirst, autovalue);

            }
            catch (Exception ex)
            {
                //Serilog.Log.Error(ex, ex.Message);
                return null;
            }




            return res;
        }

        //优化尾部有一条线的情形，换一个算法
        public MJietiaoShowResultFSK2 JietiaoFSK3_2(List<double> orgYvaluess, int samplefre, int fre1, int bandwidth1, int order1, int fre2, int bandwidth2, int order2, int shift, int signalFre, bool isGuiyi, bool isAutoFirst, int autovalue, double midValue, double checkValue)
        {
            MJietiaoShowResultFSK2 res = new MJietiaoShowResultFSK2();

            try
            {
                #region 找到有效的数据段
                List<double> effyvalues = new List<double>();

                int checkCntEnd = samplefre / fre2;
                double maxCheck = midValue + checkValue;
                double minCheck = midValue - checkValue;
                List<double> org = orgYvaluess.Where(w => w > minCheck && w < maxCheck).ToList();
                bool isFindStart = false;
                for (int i = 0; i < orgYvaluess.Count; i++)
                {
                    if (!isFindStart)
                    {
                        //bool isOk = true;
                        double cmax = -10000;
                        double cmin = 100000;
                        for (int k = 0; k < checkCntEnd; k++)
                        {
                            if (i + k >= orgYvaluess.Count)
                            {
                                break;
                            }
                            if (orgYvaluess[i + k] > cmax)
                            {
                                cmax = orgYvaluess[i + k];
                            }
                            if (orgYvaluess[i + k] < cmin)
                            {
                                cmin = orgYvaluess[i + k];
                            }
                        }

                        if (cmax - cmin >= checkValue)
                        {
                            isFindStart = true;

                            effyvalues.Add(orgYvaluess[i + checkCntEnd - 1]);
                        }
                    }
                    else
                    {

                        double cmax = -1000;
                        double cmin = 100000;
                        for (int k = 0; k < checkCntEnd; k++)
                        {
                            if (i + k >= orgYvaluess.Count)
                            {
                                break;
                            }

                            if (orgYvaluess[i + k] > cmax)
                            {
                                cmax = orgYvaluess[i + k];
                            }
                            if (orgYvaluess[i + k] < cmin)
                            {
                                cmin = orgYvaluess[i + k];
                            }
                        }

                        if (cmax - cmin > checkValue)
                        {
                            effyvalues.Add(orgYvaluess[i]);
                        }
                        else
                        {
                            break;
                        }

                    }
                }



                #endregion

                #region 找到有效的数据段2

                //List<double> effyvalues = new List<double>();
                //effyvalues = orgYvaluess.Skip(40000).Take(80000).ToList();

                #endregion

                res = JietiaoFSK2(effyvalues, samplefre, fre1, bandwidth1, order1, fre2, bandwidth2, order2, shift, signalFre, isGuiyi, isAutoFirst, autovalue);

            }
            catch (Exception ex)
            {
                Serilog.Log.Error(ex, ex.Message);
                return null;
            }

            return res;
        }


        /// <summary>
        /// 生成一秒的数据
        /// </summary>
        /// <param name="signalFre">信号频率</param>
        /// <param name="sampleFre">采样率</param>
        /// <param name="fre1">频率1,低频</param>
        /// <param name="amp1">频率1幅度</param>
        /// <param name="offset1">频率1基准</param>
        /// <param name="fre2">频率2,高频</param>
        /// <param name="amp2">频率2幅度</param>
        /// <param name="offset2">频率2基准</param>
        /// <param name="signalType">信号类型，1=010101...,2=随机</param>
        /// <returns></returns>
        public List<double> CalSingal(int signalFre, int sampleFre, int fre1, double amp1, double offset1, int fre2, double amp2, double offset2, int signalType)
        {
            List<double> res = new List<double>();

            List<int> numberSignal = new List<int>();
            if (true)
            {
                int n0 = sampleFre / signalFre;
                int lastIndex = -1;
                int currentValue = 0;
                for (int i = 0; i < sampleFre; i++)
                {
                    int cindex = i / n0;
                    if (cindex != lastIndex)
                    {
                        lastIndex = cindex;

                        currentValue = cindex % 2;

                        if (signalType == 2)
                        {
                            currentValue = _rand.Next(0, 100) % 2;
                        }
                    }

                    numberSignal.Add(currentValue);
                }

            }



            List<double> data1 = new List<double>();
            List<double> data2 = new List<double>();
            if (true)
            {
                for (int i = 0; i < sampleFre; i++)
                {
                    double cv1 = offset1 + amp1 * Math.Sin(Math.PI * 2.0 * i * fre1 / sampleFre);

                    double cv2 = offset2 + amp2 * Math.Sin(Math.PI * 2.0 * i * fre2 / sampleFre);

                    data1.Add(cv1);
                    data2.Add(cv2);

                }
            }

            for (int i = 0; i < sampleFre; i++)
            {
                if (numberSignal[i] == 0)
                {
                    res.Add(data1[i]);
                }
                else
                {
                    res.Add(data2[i]);
                }
            }


            return res;
        }


        /// <summary>
        /// 生成一秒的数据
        /// </summary>
        /// <param name="signalFre">信号频率</param>
        /// <param name="sampleFre">采样率</param>
        /// <param name="fre1">频率1,低频</param>
        /// <param name="amp1">频率1幅度</param>
        /// <param name="offset1">频率1基准</param>
        /// <param name="fre2">频率2,高频</param>
        /// <param name="amp2">频率2幅度</param>
        /// <param name="offset2">频率2基准</param>
        /// <param name="signalType">信号类型，1=010101...,2=随机</param>
        /// <returns></returns>
        public CalSingalInfo CalSingal2(int signalFre, int sampleFre, int fre1, double amp1, double offset1, int fre2, double amp2, double offset2, int signalType, string signalsequence)
        {
            List<double> res = new List<double>();

            List<int> numbers = new List<int>();

            List<int> numberSignal = new List<int>();
            if (true)
            {
                int n0 = sampleFre / signalFre;
                int lastIndex = -1;
                int currentValue = 0;
                int index = 0;
                for (int i = 0; i < sampleFre; i++)
                {
                    int cindex = i / n0;
                    if (cindex != lastIndex)
                    {
                        lastIndex = cindex;

                        currentValue = cindex % 2;

                        if (signalType == 2)
                        {
                            currentValue = _rand.Next(0, 100) % 2;
                        }
                        else if (signalType == 3)
                        {
                            //索引
                            //int index = (int)Math.Floor(Convert.ToDouble(i / n0));
                            //取值
                            string signalsequenceSelect = signalsequence[index].ToString();
                            currentValue = Convert.ToInt32(signalsequenceSelect);
                            index++;

                            if (index >= signalsequence.Length)
                            {
                                index = 0;
                            }
                        }

                        numbers.Add(currentValue);
                    }

                    numberSignal.Add(currentValue);
                }

            }



            List<double> data1 = new List<double>();
            List<double> data2 = new List<double>();
            if (true)
            {
                for (int i = 0; i < sampleFre; i++)
                {
                    double cv1 = offset1 + amp1 * Math.Sin(Math.PI * 2.0 * i * fre1 / sampleFre);

                    double cv2 = offset2 + amp2 * Math.Sin(Math.PI * 2.0 * i * fre2 / sampleFre);

                    data1.Add(cv1);
                    data2.Add(cv2);

                }
            }

            for (int i = 0; i < sampleFre; i++)
            {
                if (numberSignal[i] == 0)
                {
                    res.Add(data1[i]);
                }
                else
                {
                    res.Add(data2[i]);
                }
            }


            return new CalSingalInfo() { res = res, numberSignal = numbers, numberSignals = numberSignal };
        }

        /// <summary>
        /// 生成一秒的数据
        /// </summary>
        /// <param name="signalFre">信号频率</param>
        /// <param name="sampleFre">采样率</param>
        /// <param name="fre1">频率1,低频</param>
        /// <param name="amp1">频率1幅度</param>
        /// <param name="offset1">频率1基准</param>
        /// <param name="zkb">占空比</param>
        /// <param name="pointNumber">数据点数</param>
        /// <returns></returns>
        public CalSingalInfo CalSingalASK(int signalFre, int sampleFre, int fre1, double amp1, double offset1, double zkb, int pointNumber, int signalType, string signalsequence)
        {
            List<double> res = new List<double>();


            List<int> numberSignal = new List<int>();
            List<int> numberSignals = new List<int>();


            if (true)
            {
                int n0 = sampleFre / signalFre;
                int n1 = (int)(n0 * zkb);

                int currentValue = 0;
                int tmpValue = 1;
                int index = 0;
                int lastFlag = 1;
                for (int i = 0; i < pointNumber; i++)
                {

                    // 随机
                    if (signalType == 2)
                    {
                        int cindex2 = i % n0;
                        if (cindex2 == 0)
                        {
                            tmpValue = _rand.Next(0, 100) % 2;


                            numberSignals.Add(tmpValue);
                        }

                        if (cindex2 < n0)
                        {
                            currentValue = tmpValue;
                        }
                        else
                        {
                            currentValue = 0;
                        }

                    }
                    // 手动输入
                    else if (signalType == 3)
                    {
                        int cindex2 = i % n0;
                        if (cindex2 == 0)
                        {
                            //索引
                            //int index = (int)Math.Floor(Convert.ToDouble(i / n0));
                            //取值
                            string signalsequenceSelect = signalsequence[index].ToString();
                            tmpValue = Convert.ToInt32(signalsequenceSelect);
                            index++;

                            if (index >= signalsequence.Length)
                            {
                                index = 0;
                            }

                            numberSignals.Add(tmpValue);
                        }

                        if (cindex2 < n0)
                        {
                            currentValue = tmpValue;
                        }
                        else
                        {
                            currentValue = 0;
                        }
                    }
                    // 10
                    else
                    {
                        int cindex2 = i % n0;
                        if (cindex2 == 0)
                        {
                            if (lastFlag == 0)
                            {
                                lastFlag = 1;
                            }
                            else
                            {
                                lastFlag = 0;
                            }

                            numberSignals.Add(lastFlag);
                        }

                        currentValue = lastFlag;

                        //if (cindex2 < n1)
                        //{
                        //    currentValue = 1;
                        //}
                        //else
                        //{
                        //    currentValue = 0;
                        //}
                    }

                    numberSignal.Add(currentValue);
                }
            }


            List<double> data1 = new List<double>();

            if (true)
            {
                for (int i = 0; i < pointNumber; i++)
                {
                    double cv1 = offset1 + amp1 * Math.Sin(Math.PI * 2.0 * i * fre1 / sampleFre);
                    data1.Add(cv1);

                }
            }

            for (int i = 0; i < pointNumber; i++)
            {
                if (numberSignal[i] == 1)
                {
                    res.Add(data1[i]);
                }
                else
                {
                    res.Add(0);
                }
            }


            return new CalSingalInfo() { res = res, numberSignal = numberSignal, numberSignals = numberSignals };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="signalFre"></param>
        /// <param name="sampleFre"></param>
        /// <param name="fre1"></param>
        /// <param name="amp1"></param>
        /// <param name="offset1"></param>
        /// <param name="phase">相位，单位度</param>
        /// <param name="zkb">占空比</param>
        /// <param name="pointNumber"></param>
        /// <returns></returns>
        public CalSingalInfo CalSingalASK(int signalFre, int sampleFre, int fre1, double amp1, double offset1, double phase, double zkb, int pointNumber, int signalType, string signalsequence)
        {
            List<double> res = new List<double>();


            List<int> numberSignal = new List<int>();
            List<int> numberSignals = new List<int>();



            //if (true)
            //{
            //    int n0 = sampleFre / signalFre;
            //    int n1 = (int)(n0 * zkb);

            //    int currentValue = 0;
            //    for (int i = 0; i < pointNumber; i++)
            //    {
            //        int cindex2 = i % n0;

            //        if (cindex2 < n1)
            //        {
            //            currentValue = 1;
            //        }
            //        else
            //        {
            //            currentValue = 0;
            //        }

            //        numberSignal.Add(currentValue);
            //    }

            //}
            if (true)
            {
                int n0 = sampleFre / signalFre;
                int n1 = (int)(n0 * zkb);

                int currentValue = 0;
                int tmpValue = 1;
                int index = 0;
                for (int i = 0; i < pointNumber; i++)
                {
                    // 随机
                    if (signalType == 2)
                    {
                        int cindex2 = i % n0;
                        if (cindex2 == 0)
                        {
                            tmpValue = _rand.Next(0, 100) % 2;

                            numberSignals.Add(tmpValue);
                        }

                        if (cindex2 < n1)
                        {
                            currentValue = tmpValue;
                        }
                        else
                        {
                            currentValue = 0;
                        }
                    }
                    // 手动输入
                    else if (signalType == 3)
                    {
                        int cindex2 = i % n0;
                        if (cindex2 == 0)
                        {
                            //索引
                            //int index = (int)Math.Floor(Convert.ToDouble(i / n0));
                            //取值
                            string signalsequenceSelect = signalsequence[index].ToString();
                            tmpValue = Convert.ToInt32(signalsequenceSelect);
                            index++;

                            if (index >= signalsequence.Length)
                            {
                                index = 0;
                            }

                            numberSignals.Add(tmpValue);
                        }

                        if (cindex2 < n1)
                        {
                            currentValue = tmpValue;
                        }
                        else
                        {
                            currentValue = 0;
                        }
                    }
                    // 01
                    else if(signalType == 1)
                    {
                        //int cindex2 = i % n0;
                        //if (cindex2 == 0)
                        //{
                        //    if (currentValue == 0)
                        //    {
                        //        currentValue = 1;
                        //    }
                        //    else
                        //    {
                        //        currentValue = 0;
                        //    }

                        //    numberSignals.Add(currentValue);
                        //}

                        int cindex2 = i % n0;
                        if (cindex2 == 0)
                        {
                            if (currentValue == 0)
                            {
                                currentValue = 1;
                            }
                            else
                            {
                                currentValue = 0;
                            }
                            numberSignals.Add(currentValue);
                        }
                        else
                        {
                            if(cindex2>=n1)
                            {
                                currentValue = 0;
                            }
                        }
                    }


                    numberSignal.Add(currentValue);
                }
            }



            List<double> data1 = new List<double>();

            if (true)
            {
                double nphase = phase * Math.PI / 180.0;
                for (int i = 0; i < pointNumber; i++)
                {
                    double cv1 = offset1 + amp1 * Math.Sin(Math.PI * 2.0 * i * fre1 / sampleFre + nphase);
                    data1.Add(cv1);

                }
            }

            for (int i = 0; i < pointNumber; i++)
            {
                if (numberSignal[i] == 1)
                {
                    res.Add(data1[i]);
                }
                else
                {
                    res.Add(0);
                }
            }


            return new CalSingalInfo() { res = res, numberSignal = numberSignal , numberSignals = numberSignals };
        }

        public CalSingalInfo CalSingalASK(int signalFre, int sampleFre, int fre1, double amp1, double offset1, double phase, double zkb, int pointNumber, int signalType, List<int> numberSignal, List<int> numberSignals)
        {
            List<double> res = new List<double>();




            //if (true)
            //{
            //    int n0 = sampleFre / signalFre;
            //    int n1 = (int)(n0 * zkb);

            //    int currentValue = 0;
            //    for (int i = 0; i < pointNumber; i++)
            //    {
            //        int cindex2 = i % n0;

            //        if (cindex2 < n1)
            //        {
            //            currentValue = 1;
            //        }
            //        else
            //        {
            //            currentValue = 0;
            //        }

            //        numberSignal.Add(currentValue);
            //    }

            //}
            if (false)
            {
                int n0 = sampleFre / signalFre;
                int n1 = (int)(n0 * zkb);

                int currentValue = 0;
                int tmpValue = 1;
                for (int i = 0; i < pointNumber; i++)
                {

                    if (signalType == 2)
                    {
                        int cindex2 = i % n0;
                        if (cindex2 == 0)
                        {
                            tmpValue = _rand.Next(0, 100) % 2;
                        }

                        if (cindex2 < n1)
                        {
                            currentValue = tmpValue;
                        }
                        else
                        {
                            currentValue = 0;
                        }

                    }
                    else
                    {
                        int cindex2 = i % n0;
                        if (cindex2 < n1)
                        {
                            currentValue = 1;
                        }
                        else
                        {
                            currentValue = 0;
                        }
                    }

                    numberSignal.Add(currentValue);
                }
            }


            List<double> data1 = new List<double>();

            if (true)
            {
                double nphase = phase * Math.PI / 180.0;
                for (int i = 0; i < pointNumber; i++)
                {
                    double cv1 = offset1 + amp1 * Math.Sin(Math.PI * 2.0 * i * fre1 / sampleFre + nphase);
                    data1.Add(cv1);

                }
            }

            for (int i = 0; i < pointNumber; i++)
            {
                if (numberSignal[i] == 1)
                {
                    res.Add(data1[i]);
                }
                else
                {
                    res.Add(0);
                }
            }


            return new CalSingalInfo() { res = res, numberSignal = numberSignal, numberSignals = numberSignals };
        }

        /// <summary>
        /// 生成一秒的数据
        /// </summary>
        /// <param name="signalFre">信号频率</param>
        /// <param name="sampleFre">采样率</param>
        /// <param name="fre1">频率1,低频</param>
        /// <param name="amp1">频率1幅度</param>
        /// <param name="offset1">频率1基准</param>
        /// <param name="fre2">频率2,高频</param>
        /// <param name="amp2">频率2幅度</param>
        /// <param name="offset2">频率2基准</param>
        /// <param name="signalType">信号类型，1=010101...,2=随机</param>
        /// <returns></returns>
        public CalSingalInfo CalSingal2(int signalFre, int sampleFre, int fre1, double amp1, double offset1, double phase1, int fre2, double amp2, double offset2, double phase2, int signalType, string signalsequence)
        {
            List<double> res = new List<double>();

            List<int> numbers = new List<int>();

            List<int> numberSignal = new List<int>();
            if (true)
            {
                int n0 = sampleFre / signalFre;
                int lastIndex = -1;
                int currentValue = 0;
                int index = 0;
                for (int i = 0; i < sampleFre; i++)
                {
                    int cindex = i / n0;
                    if (cindex != lastIndex)
                    {
                        lastIndex = cindex;

                        currentValue = cindex % 2;

                        //随机
                        if (signalType == 2)
                        {
                            currentValue = _rand.Next(0, 100) % 2;
                        }
                        //手动输入
                        else if (signalType == 3)
                        {
                            //索引
                            //int index = (int)Math.Floor(Convert.ToDouble(i / n0));
                            //取值
                            string signalsequenceSelect = signalsequence[index].ToString();
                            currentValue = Convert.ToInt32(signalsequenceSelect);
                            index++;

                            if (index >= signalsequence.Length)
                            {
                                index = 0;
                            }
                        }

                        numbers.Add(currentValue);
                    }

                    numberSignal.Add(currentValue);
                }

            }



            List<double> data1 = new List<double>();
            List<double> data2 = new List<double>();
            if (true)
            {
                double nphase1 = phase1 * Math.PI / 180.0;
                double nphase2 = phase2 * Math.PI / 180.0;

                for (int i = 0; i < sampleFre; i++)
                {
                    double cv1 = offset1 + amp1 * Math.Sin(Math.PI * 2.0 * i * fre1 / sampleFre + nphase1);

                    double cv2 = offset2 + amp2 * Math.Sin(Math.PI * 2.0 * i * fre2 / sampleFre + nphase2);

                    data1.Add(cv1);
                    data2.Add(cv2);

                }
            }

            for (int i = 0; i < sampleFre; i++)
            {
                if (numberSignal[i] == 0)
                {
                    res.Add(data1[i]);
                }
                else
                {
                    res.Add(data2[i]);
                }
            }


            return new CalSingalInfo() { res = res, numberSignal = numbers };
        }

        /// <summary>
        /// FSK两路输出
        /// </summary>
        /// <param name="signalFre"></param>
        /// <param name="sampleFre"></param>
        /// <param name="fre1"></param>
        /// <param name="amp1"></param>
        /// <param name="offset1"></param>
        /// <param name="phase"></param>
        /// <param name="fre2"></param>
        /// <param name="amp2"></param>
        /// <param name="offset2"></param>
        /// <param name="phase2"></param>
        /// <param name="signalType"></param>
        /// <param name="signalsequence"></param>
        /// <returns></returns>
        public CalSingalInfo CalSingalFSK2(int signalFre, int signalnumber, int fre1, double amp1, double offset1, int fre2, double amp2, double offset2, double phase, int signalType, string signalsequence, double zkb)
        {
            try
            {
                List<double> res = new List<double>();

                List<int> numbers = new List<int>();

                int n0 = signalnumber / signalFre;
                int n1 = (int)(n0 * zkb);

                List<int> numberSignal = new List<int>();
                if (true)
                {

                    int lastIndex = -1;
                    int currentValue = 0;
                    int index = 0;
                    for (int i = 0; i < signalnumber; i++)
                    {
                        int cindex = i / n0;

                        if (cindex != lastIndex)
                        {
                            lastIndex = cindex;


                            currentValue = cindex % 2;

                            //随机
                            if (signalType == 2)
                            {
                                currentValue = _rand.Next(0, 100) % 2;
                            }
                            //手动输入
                            else if (signalType == 3)
                            {
                                //索引
                                //int index = (int)Math.Floor(Convert.ToDouble(i / n0));
                                //取值
                                string signalsequenceSelect = signalsequence[index].ToString();
                                currentValue = Convert.ToInt32(signalsequenceSelect);
                                index++;

                                if (index >= signalsequence.Length)
                                {
                                    index = 0;
                                }
                            }

                            numbers.Add(currentValue);
                        }

                        numberSignal.Add(currentValue);
                    }

                }



                List<double> data1 = new List<double>();
                List<double> data2 = new List<double>();
                if (true)
                {
                    double nphase1 = phase * Math.PI / 180.0;
                    //double nphase2 = phase2 * Math.PI / 180.0;

                    for (int i = 0; i < signalnumber; i++)
                    {
                        double cv1 = offset1 + amp1 * Math.Sin(Math.PI * 2.0 * i * fre1 / signalnumber + nphase1);

                        double cv2 = offset2 + amp2 * Math.Sin(Math.PI * 2.0 * i * fre2 / signalnumber + nphase1);

                        data1.Add(cv1);
                        data2.Add(cv2);

                    }
                }



                for (int i = 0; i < signalnumber; i++)
                {
                    int tmpIndex = i % n0;

                    if (numberSignal[i] == 0)
                    {
                        if (tmpIndex < n1)
                        {
                            res.Add(data1[i]);
                        }
                        else
                        {
                            res.Add(0);
                        }
                    }
                    else
                    {
                        if (tmpIndex < n1)
                        {
                            res.Add(data2[i]);
                        }
                        else
                        {
                            res.Add(0);
                        }

                    }
                }


                return new CalSingalInfo() { res = res, numberSignal = numberSignal };
            }
            catch (Exception ex)
            {
                Log.Error(ex, ex.Message);
                return null;
            }
        }

        public CalSingalInfo CalSingalFSK2(int signalFre, int signalnumber, int fre1, double amp1, double offset1, int fre2, double amp2, double offset2, double phase, List<int> numberSignal, double zkb)
        {
            List<double> res = new List<double>();

            List<int> numbers = new List<int>();


            int n0 = signalnumber / signalFre;
            int n1 = (int)(n0 * zkb);


            List<double> data1 = new List<double>();
            List<double> data2 = new List<double>();
            if (true)
            {
                double nphase1 = phase * Math.PI / 180.0;
                //double nphase2 = phase2 * Math.PI / 180.0;

                for (int i = 0; i < signalnumber; i++)
                {
                    double cv1 = offset1 + amp1 * Math.Sin(Math.PI * 2.0 * i * fre1 / signalnumber + nphase1);

                    double cv2 = offset2 + amp2 * Math.Sin(Math.PI * 2.0 * i * fre2 / signalnumber + nphase1);

                    data1.Add(cv1);
                    data2.Add(cv2);

                }
            }

            for (int i = 0; i < signalnumber; i++)
            {
                int tmpIndex = i % n0;

                if (numberSignal[i] == 0)
                {
                    if (tmpIndex < n1)
                    {
                        res.Add(data1[i]);

                    }
                    else
                    {
                        res.Add(0);
                    }
                }
                else
                {
                    if (tmpIndex < n1)
                    {
                        res.Add(data2[i]);
                    }
                    else
                    {
                        res.Add(0);
                    }
                }
            }
            numbers = numberSignal;

            return new CalSingalInfo() { res = res, numberSignal = numbers };
        }


        /// <summary>
        /// 生成一秒的数据
        /// </summary>
        /// <param name="signalFre">信号频率</param>
        /// <param name="sampleFre">采样率</param>
        /// <param name="fre1">频率1,低频</param>
        /// <param name="amp1">频率1幅度</param>
        /// <param name="offset1">频率1基准</param>
        /// <param name="fre2">频率2,高频</param>
        /// <param name="amp2">频率2幅度</param>
        /// <param name="offset2">频率2基准</param>
        /// <param name="signalType">信号类型，1=010101...,2=随机</param>
        /// <returns></returns>
        public CalSingalInfo CalSingal2(int signalFre, int sampleFre, int fre1, double amp1, double offset1, double phase1, int fre2, double amp2, double offset2, double phase2, int signalType, List<int> numberSignal)
        {
            List<double> res = new List<double>();

            List<int> numbers = new List<int>();

            //List<int> numberSignal = new List<int>();
            //if (false)
            //{
            //    int n0 = sampleFre / signalFre;
            //    int lastIndex = -1;
            //    int currentValue = 0;
            //    int index = 0;
            //    for (int i = 0; i < sampleFre; i++)
            //    {
            //        int cindex = i / n0;
            //        if (cindex != lastIndex)
            //        {
            //            lastIndex = cindex;

            //            currentValue = cindex % 2;

            //            //随机
            //            if (signalType == 2)
            //            {
            //                currentValue = _rand.Next(0, 100) % 2;
            //            }
            //            //手动输入
            //            else if (signalType == 3)
            //            {
            //                //索引
            //                //int index = (int)Math.Floor(Convert.ToDouble(i / n0));
            //                //取值
            //                string signalsequenceSelect = signalsequence[index].ToString();
            //                currentValue = Convert.ToInt32(signalsequenceSelect);
            //                index++;

            //                if (index >= signalsequence.Length)
            //                {
            //                    index = 0;
            //                }
            //            }

            //            numbers.Add(currentValue);
            //        }

            //        numberSignal.Add(currentValue);
            //    }

            //}

            List<double> data1 = new List<double>();
            List<double> data2 = new List<double>();
            if (true)
            {
                double nphase1 = phase1 * Math.PI / 180.0;
                double nphase2 = phase2 * Math.PI / 180.0;

                for (int i = 0; i < sampleFre; i++)
                {
                    double cv1 = offset1 + amp1 * Math.Sin(Math.PI * 2.0 * i * fre1 / sampleFre + nphase1);

                    double cv2 = offset2 + amp2 * Math.Sin(Math.PI * 2.0 * i * fre2 / sampleFre + nphase2);

                    data1.Add(cv1);
                    data2.Add(cv2);

                }
            }

            for (int i = 0; i < sampleFre; i++)
            {
                if (numberSignal[i] == 0)
                {
                    res.Add(data1[i]);
                }
                else
                {
                    res.Add(data2[i]);
                }
            }


            return new CalSingalInfo() { res = res, numberSignal = numbers };
        }


    }

    public class MJietiaoShowResultFSK
    {

        public List<double> Times = new List<double>();   //横坐标时间轴数据，如果没有X，就用这个

        public List<double> YValues = new List<double>(); //有效数据Y

        public List<double> Orig_Y1 = new List<double>(); //频率1 滤波后原始数据Y
        public List<double> FFT_X1 = new List<double>();  //频率1 FFT的x坐标
        public List<double> FFT_Y1 = new List<double>();  //频率1 FFT的y坐标
        public List<double> Bao_Y1 = new List<double>();  //频率1 包络数据Y

        public List<double> Orig_Y2 = new List<double>(); //频率2 滤波后原始数据Y
        public List<double> FFT_X2 = new List<double>();  //频率2 FFT的x坐标
        public List<double> FFT_Y2 = new List<double>();  //频率2 FFT的y坐标
        public List<double> Bao_Y2 = new List<double>();  //频率2 包络数据Y

        public List<int> Judge_X = new List<int>();    //判别后横坐标为个数的
        public List<int> Judge_Y = new List<int>();    //判别后纵坐标

        public List<int> ChouYang_X = new List<int>();  //抽样点的X
        public List<int> ChouYang_Y = new List<int>();  //抽样点的Y

        public List<int> JudgeResult = new List<int>(); //判别后的序列，不用显示

        public MResultInfo ResultInfo = new MResultInfo(); //解调结果

        private void UpdateResultInfo()
        {
            ResultInfo.JudgeResult = JudgeResult;
        }

        public void AutoFirst(bool isAuotFisrt, int autovalue = 0)
        {
            if (isAuotFisrt)
            {
                JudgeResult.Insert(0, autovalue);

                //if (JudgeResult.Count>0)
                //{
                //    if(JudgeResult[0]==0)
                //    {

                //    }
                //    else
                //    {
                //        JudgeResult.Insert(0, 0);
                //    }
                //}
            }

            UpdateResultInfo();
        }
    }

    public class MResultInfo
    {
        public List<int> JudgeResult = new List<int>();

        /// <summary>
        /// 总数
        /// </summary>
        public int TotalCount
        {
            get { return JudgeResult.Count; }
        }

        /// <summary>
        /// 1的个数
        /// </summary>
        public int OneCount
        {
            get { return JudgeResult.Sum(); }
        }

        /// <summary>
        /// 0的个数
        /// </summary>
        public int ZeroCount
        {
            get { return TotalCount - OneCount; }
        }

        /// <summary>
        /// 获取数字信号
        /// </summary>
        /// <returns></returns>
        public string GetDigitalSignal()
        {
            string res = null;
            for (int i = 0; i < JudgeResult.Count; i++)
            {
                res += JudgeResult[i].ToString();
            }

            return res;
        }

        /// <summary>
        /// 获取结果序列字符串
        /// </summary>
        /// <returns></returns>
        public string GetString(double rate)
        {
            string res = string.Format("总数：{0}，1个数：{1}，0个数：{2}，误码率：{3}%\r\n", TotalCount, OneCount, ZeroCount, rate);

            for (int i = 0; i < JudgeResult.Count; i++)
            {
                res += JudgeResult[i].ToString();
            }

            return res;
        }
    }
}
